<?php
/*
 * @Author: your name
 * @Date: 2020-10-20 09:33:05
 * @LastEditTime: 2021-01-05 11:22:36
 * @LastEditors: Please set LastEditors
 * @Description: 虚拟规则验证器
 * @FilePath: /special/application/adminapiv3/validate/FirewallRuleValidate.php
 */
namespace  app\adminapiv3\validate;

use think\Validate;

class FirewallRuleValidate extends Validate
{
    protected $rule = [
        //源ip
        'src_ip|源IP' => [
            'require',
            'regex:/^any|^\d{1,3}(\.\d{1,3}){3}([,-]\d{1,3}(\.\d{1,3}){3}){0,11}$/i'
        ],
        //目标ip
        'dest_ip|目标IP' => [
            'require',
            'regex:/^any|^\d{1,3}(\.\d{1,3}){3}([,-]\d{1,3}(\.\d{1,3}){3}){0,11}$/i',
        ],
        //源端口
        'src_port|源端口' => [
            'require',
            'regex:/^any|^\d{1,6}([,:]\d{1,6}){0,14}$/i',
        ],
        //目标端口
        'dest_port|目标端口' => [
            'require',
            'regex:/^any|^\d{1,6}([,:]\d{1,6}){0,14}$/i',
        ],
        //是否记录 1记录 0不记录
        'have_mac_addr|记录MAC值' => 'in:0,1',
        //状态 0不启用 1启用
        'status|规则状态' => 'require|in:0,1',
        //处理方式 0拒绝 1通过
        'process|规则处理方式' => 'require|in:0,1',
        //方向 1出站 2入站
        'direction|流量方向' => 'require|in:1,2',
        //别名
        'alias|规则别名' => 'require|length:1,255',
        //规则名
        'rule_name|规则别名' => 'require|length:1,255',
        //主机id
        'host_id|主机ID' => 'require|regex:^\d+(,\d+){0,30}$',
        //规则类型 1手动 2自动
        'type|规则类型' => 'require|in:1,2',
        //是否强制下发 0不强制 1强制
        'force|是否强制下发' => 'in:0,1',
        //规则id
        'id|规则ID' => 'require|regex:^\d+(,\d+){0,30}',
        //主规则id
        'fid|主规则ID' => 'require|regex:^\d+(,\d+){0,30}',
        //MAC地址
        'mac_addr|MAC地址' => 'regex:^\w{32}',
        //学习数据id
        'learn_id|学习数据ID' => 'require|number|length:1,11',
        //协议类型
        'protocols|协议类型' => 'require|number|in:6,17,255',
        /*
            规则应用范围
            windows 公有网络 public 私有网络 private 域 domain 
            linux  外网区域 8 extranet 内网区域  5 intranet 通信区域 3 connect 默认区域 default 11
        */
        'scope|规则应用范围' => [
            'require',
            'regex:^((any|(public)?(private|,private)?(domain|,domain)?)|(5|8|3|11))$'
        ],
    ];

    //错误消息
    protected $message = [
        'have_mac_addr.in' => '记录MAC值格式错误',
        'status.in' => '规则状态格式错误',
        'process.in' => '规则处理方式格式错误',
        'direction.in' => '流量方向格式错误',
        'type.in' => '规则类型格式错误',
        'force.in' => '是否强制下发格式错误',
        'protocols.in' => '协议类型错误',
    ];

    protected $scene = [
        //列表筛选
        // 'listFilter' => ['src_ip','dest_ip','dest_port','src_port','have_mac_addr','status','process','protocols','direction','alias'],
        //添加规则
        'add' => ['host_id','type','src_ip','dest_ip','dest_port','src_port','process','protocols','direction','alias','force','learn_id','scope'],
        //修改规则
        'modify' => ['host_id','src_ip','dest_ip','dest_port','src_port','process','protocols','direction','alias','fid','learn_id','scope'],
        'fidRequire' => ['fid','host_id'],
    ];

    //列表筛选
    public function sceneListFilter ()
    {
       return $this->only([
           'src_ip','dest_ip','dest_port',
           'src_port','have_mac_addr','status',
           'process','protocols','direction',
           'alias','host_id','type',
           'scope'
        ])
        ->remove('src_ip', 'require')
        ->remove('dest_ip', 'require')
        ->remove('src_port', 'require')
        ->remove('dest_port', 'require')
        ->remove('have_mac_addr', 'require')
        ->remove('status', 'require')
        ->remove('process', 'require')
        ->remove('direction', 'require')
        ->remove('alias', 'require')
        ->remove('type', 'require')
        ->remove('scope', 'require')
        ->remove('protocols', 'require');
    }

    /**
     * @name: 开启关闭规则场景
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-10 16:59:41
     */
    public function sceneOnOff ()
    {
        return $this->only(['host_id','fid'])
        ->remove('host_id', 'regex')
        ->append('host_id', 'number');
    }

    /**
     * @name: 日志列表筛选
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-11-25 13:49:30
     */
    public function sceneLogListFilter()
    {
       return $this->only(['src_ip','dest_ip','dest_port','src_port','direction','rule_name','host_id'])
        ->remove('src_ip', 'require')
        ->remove('dest_ip', 'require')
        ->remove('src_port', 'require')
        ->remove('dest_port', 'require')
        ->remove('direction', 'require')
        ->remove('rule_name', 'require');
    }
}